package com.util;

import java.awt.*;

/**
 * 编写四方格的父类
 *      属性：Cell[]
 *      方法：整体左右下移动一格，变形
 * @author congw
 */
public class Tetromino {
    protected Cell[] cells = new Cell[4];

    /**
     * 旋转状态
     */
    public State[] states;

    /**
     * 旋转次数
     */
    protected int count = 10000;

    /**
     * 左移动
     */
    public void leftMove(){
        for (Cell cell : cells) {
            cell.left();
        }
    }
    //
    public void rightMove(){
        for (Cell cell : cells) {
            cell.right();
        }
    }
    //
    public void downMove(){
        for (Cell cell : cells) {
            cell.down();
        }
    }

    /**
     * 顺时针旋转四方格
     */
    public void rotateRight() {
        if (states.length == 0) {
            return;
        }
        // 旋转次数+1
        count ++;
        State s = states[count % states.length];
        Cell cell = cells[0];
        int row = cell.getRow();
        int col = cell.getCol();
        cells[1].setRow(row + s.row1);
        cells[1].setCol(col + s.col1);
        cells[2].setRow(row + s.row2);
        cells[2].setCol(col + s.col2);
        cells[3].setRow(row + s.row3);
        cells[3].setCol(col + s.col3);
    }

    /**
     * 逆时针旋转
     */
    public void rotateLeft() {
        if (states.length == 0) {
            return;
        }
        // 旋转次数-1
        count --;
        State s = states[count % states.length];
        Cell cell = cells[0];
        int row = cell.getRow();
        int col = cell.getCol();
        cells[1].setRow(row + s.row1);
        cells[1].setCol(col + s.col1);
        cells[2].setRow(row + s.row2);
        cells[2].setCol(col + s.col2);
        cells[3].setRow(row + s.row3);
        cells[3].setCol(col + s.col3);
    }

    /**
     * 随机生成一个
     * @return 返回一个Tetromino
     */
    public static Tetromino randomOne(){
        int num = (int)(Math.random() * 7);
        Tetromino res = null;
        switch (num){
            case 0:
                res = new I();
                break;
            case 1:
                res = new J();
                break;
            case 2:
                res = new S();
                break;
            case 3:
                res = new Z();;
                break;
            case 4:
                res = new T();
                break;
            case 5:
                res = new O();
                break;
            case 6:
                res = new L();
                break;
            default:
        }
        return res;
    }

    public Cell[] getCells() {
        return cells;
    }

    public void setCells(Cell[] cells) {
        this.cells = cells;
    }


    public class State{
        /**
         * 四个方块的相对位置
         */
        private int row0, col0, row1, col1, row2, col2, row3, col3;

        public State(){
        }

        public State(int row0, int col0, int row1, int col1, int row2, int col2, int row3, int col3) {
            this.row0 = row0;
            this.col0 = col0;
            this.row1 = row1;
            this.col1 = col1;
            this.row2 = row2;
            this.col2 = col2;
            this.row3 = row3;
            this.col3 = col3;
        }

        public int getRow0() {
            return row0;
        }

        public void setRow0(int row0) {
            this.row0 = row0;
        }

        public int getCol0() {
            return col0;
        }

        public void setCol0(int col0) {
            this.col0 = col0;
        }

        public int getRow1() {
            return row1;
        }

        public void setRow1(int row1) {
            this.row1 = row1;
        }

        public int getCol1() {
            return col1;
        }

        public void setCol1(int col1) {
            this.col1 = col1;
        }

        public int getRow2() {
            return row2;
        }

        public void setRow2(int row2) {
            this.row2 = row2;
        }

        public int getCol2() {
            return col2;
        }

        public void setCol2(int col2) {
            this.col2 = col2;
        }

        public int getRow3() {
            return row3;
        }

        public void setRow3(int row3) {
            this.row3 = row3;
        }

        public int getCol3() {
            return col3;
        }

        public void setCol3(int col3) {
            this.col3 = col3;
        }

        @Override
        public String toString() {
            return "State{" +
                    "row0=" + row0 +
                    ", col0=" + col0 +
                    ", row1=" + row1 +
                    ", col1=" + col1 +
                    ", row2=" + row2 +
                    ", col2=" + col2 +
                    ", row3=" + row3 +
                    ", col3=" + col3 +
                    '}';
        }
    }
}
